<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {

	/**
	* [index 展示用户信息]
	*/
	public function index(){

		$user = M('user');
		//获取关键字
		if(!empty($_GET['search_username'])){
			//有关键字
			$where = "username like '%".$_GET['search_username']."%'";
		}else{
			$where = '';
		}

		// 定义每页显示条目
		$num = 2;
		// 查询满足要求的总记录数
		$count = $user->where($where)->count();
		// 实例化分页类 传入总记录数和每页显示的记录数
		$Page = new \Think\Page($count,$num);

		//获取limit参数
		$limit = $Page->firstRow.','.$Page->listRows;
		// 查询zz_user表中基本字段信息, 分配给模板展示
		$res = $user->field('user_id, username, lasttime, lock, online')->where($where)->limit($limit)->select();

		// 分页显示输出
		$pages = $Page->show();
		$allpage = $Page->totalPages;
		$p = $_GET['p']?$_GET['p']:1;

		if ($p==$allpage) {
			$rows =  $count - ($allpage-1)*$num;
		}else{
			$rows = $num;
		}

		// 分配一些页码信息
		$this->assign('total',$count);
		$this->assign('rows',$rows);
		$this->assign('allpage',$allpage);
		$this->assign('p',$p);
		$this->assign('pages',$pages);
		$this->assign('res', $res);
		$this->display();
	}


	/**
	 * [add 用户添加]
	 */
	public function add(){
		// 查询标签表
		$tag = M('user_tag');
		$tag_list = $tag->select();
		$this->assign('tag', $tag_list);
		$this->display();
	}


	/**
	 * [insert 用户添加处理页面]
	 */
	public function insert(){
		if (IS_POST) {
			// 处理表单数据
			$birthday = I('post.birthday');
			$_POST['birthday'] = transfBirth($birthday);
			$_POST['password'] = md5(I('post.username'));// 默认密码为用户名
			$_POST['regtime'] = time();
			$_POST['regip'] = $_SERVER['REMOTE_ADDR'];// 添加是ip地址
			$_POST['tag_id'] = implode(',', I('post.tag_id'));
			$_POST['email_token'] = md5(I('post.username').I('post.username'));// 加密email验证码,为用户名和密码

			// 调用图片上传函数, 处理图片
			$str = Uploads('photo');
			if ($str) {
				$_POST['photo'] = $str;
			}
			// var_dump($_POST);
			// exit;
			$m = M();
			// 开启事务
			$m->startTrans();

			$user = M('user');
			$user->create();

			$id = $user->add();
			if (!$id) {
				$m->rollback();
				$this->error('添加失败');
			}

			$_POST['user_id'] = $id;
			$userinfo = M('user_info');
			$userinfo->create();
			$res = $userinfo->add();

			// 添加成功提交事务,  添加失败回滚事务
			if ($res) {
				$m->commit();
				$this->success('添加成功', U('index'));
			} else {
				$m->rollback();
				$this->error('添加失败');
			}

		}else{
			E('你访问的页面不存在');
		}
	}


	/**
	 * [edit 用户编辑页面]
	 */
	public function edit(){
		// var_dump($_GET);
		$id = $_GET['id'];
		$user = M('user');
		$res = $user->alias('u')->join('zz_user_info ui on u.user_id=ui.user_id')->where("u.user_id='$id'")->select();
		$res[0]['tag_id'] = explode(',', $res[0]['tag_id']);
		$res[0]['birthday'] = transfBirth($res[0]['birthday'], false);
		$this->assign('res', $res);

		// 查询标签表
		$tag = M('user_tag');
		$tag_list = $tag->select();
		$this->assign('tag', $tag_list);
		$this->display();
	}


	/**
	 * [editdo 用户编辑处理页面]
	 */
	public function editdo(){

		if (empty($_POST['username']) || empty($_POST['nickname'])) {
			$this->error('用户名或昵称不能为空');
		}

		if (I('post.repass')) {
			$_POST['password'] = md5(I('post.username').I('post.username'));
		}

		if ($_FILES['photo']['error'] == 0) {
			$str = Uploads('photo');
			if ($str) {
				$_POST['photo'] = $str;
			} 
		}
		// 处理表单数据
		$birthday = I('post.birthday');
		$_POST['birthday'] = transfBirth($birthday);
		$_POST['tag_id'] = implode(',', I('post.tag_id'));

		$m = M();
		// 开启事务
		$m->startTrans();

		$user = M('user');
		$user->create();

		$id = $user->save();

		$userinfo = M('user_info');
		$userinfo->create();
		$res = $userinfo->save();

		// $this->redirect();
		// 添加成功提交事务,  添加失败回滚事务
		if (!$res && !$id) {
			$m->rollback();

			$this->error('修改失败');
		} else {
			$this->success('修改成功', U('index'));
			$m->commit();
		}
	}


	/**
	* [delete 删除用户]
	*  前台发ajax请求, 提交到该控制器
	*/
	public function delete(){
		//这里请使用ajax访问
		if (IS_AJAX) {

			$m = M();
			// 开启事务
			$m->startTrans();

			$user = M('user');
			$userinfo = M('user_info');
			$deluser = $user->delete($_POST['id']);
			$deluserinfo = $userinfo->delete($_POST['id']);

			// 删除成功提交事务, 并返回ajax值1, 删除失败回滚事务, 返回ajax值0
			if ($deluser && $deluserinfo) {
				$m->commit();
				$this->ajaxReturn('1');
			} else {
				$m->rollback();
				$this->ajaxReturn('0');
			}

		}else{

			E('你访问的页面不存在');
		}
	}


	/**
	* [detail ajax请求, 返回详细信息]
	* @return [type] [description]
	*/
	public function detail(){
		if (IS_AJAX) {
			$id = I('post.id');
			$user = M('user');
			// $res = $user->join('zz_user_info on zz_user.user_id=zz_user_info.user_id')->where("zz_user.user_id='$id'")->select();
			$res = $user->alias('u')->join('zz_user_info ui on u.user_id=ui.user_id')->where("u.user_id='$id'")->select();

			// exit;
			if ($res) {
			// 查询标签
				$tag_id = $res[0]['tag_id'];
				$tag = M('user_tag');
				$data['tag_id'] = array('in', $tag_id);
				$ta = $tag->where($data)->select();
				$str = '';
				// 评价标签为字符串
				foreach ($ta as $val) {
				$str .= $val['tag_name'].',';
			}

			$res[0]['birthday'] = date('Y年m月d日', $res[0]['birthday']);
			$res[0]['regtime'] = date('Y-m-d h:i:s', $res[0]['regtime']);
			$res[0]['tag_id'] = $str;

			$this->ajaxReturn($res);
			} else {
			$this->ajaxReturn(false);
			}

		}else{
			E('你访问的页面不存在');
		}
	}

}